package com.bank.user.util;

import com.alibaba.nacos.shaded.org.checkerframework.checker.units.qual.A;
import com.bank.model.entity.*;
import com.bank.model.enums.RoleEnum;
import com.bank.model.enums.StatusEum;
import com.bank.model.vo.account.AccountVo;
import com.bank.model.vo.login.PersonalInfoVo;
import com.bank.model.vo.user.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * @author Cejay1314
 * @version 1.0
 * @create 2023/3/27 14:13
 */

@Component
public class VoFactory {
    @Autowired
    MapperFactory mapperFactory;
    Map<String,Role> rolesMap;
    MainpageVo mainpageVo;
    AccountVo accountVo;
    EmployeeVo employeeVo;
    UserVo userVo;
    DepositListVo depositListVo;
    LoanListVo loanListVo;
    PositionDistributionVo positionDistributionVo;
    UserBasicInfoVo userBasicInfoVo;



    @Autowired
    void initRolesMap(){
        rolesMap = new HashMap<String, Role>();
        List<Role> roles = mapperFactory.getRoleMapper().selectList(null);
        for (Role role:roles) {
            rolesMap.put(role.getName(),role);
        }
    }

    void initMainpageVo() {
        this.mainpageVo = new MainpageVo();
    }

    public MainpageVo getMainpageVo(User user, Role role, List<Account> accounts) {
        initMainpageVo();
        this.mainpageVo.setUserId(user.getId());
        this.mainpageVo.setUsername(user.getUsername());
        this.mainpageVo.setSex(user.getSex());
        this.mainpageVo.setHeadPortrait(user.getHeadPortrait());
        this.mainpageVo.setPhoneNumber(user.getPhoneNumber());
        this.mainpageVo.setRole(role.getName());
        this.mainpageVo.setRoleDescription(role.getDescription());
        this.mainpageVo.setAccounts(accounts);
        return this.mainpageVo;
    }
    public MainpageVo getMainpageVo(PersonalInfoVo personalInfoVo, List<Account> accounts) {
        initMainpageVo();
        this.mainpageVo.setUserId(personalInfoVo.getUserId());
        this.mainpageVo.setUsername(personalInfoVo.getUsername());
        this.mainpageVo.setSex(personalInfoVo.getSex());
        this.mainpageVo.setHeadPortrait(personalInfoVo.getHeadPortrait());
        this.mainpageVo.setPhoneNumber(personalInfoVo.getPhoneNumber());
        this.mainpageVo.setRole(personalInfoVo.getRole());
        this.mainpageVo.setRoleDescription(personalInfoVo.getRoleDescription());
        this.mainpageVo.setAccounts(accounts);
        return this.mainpageVo;
    }

    void initAccountVo(){
        this.accountVo = new AccountVo();
        this.accountVo.setType("储蓄卡");
    }
    public AccountVo getAccountVo(Account account){
        initAccountVo();
        this.accountVo.setId(account.getId());
        this.accountVo.setAccountId(account.getAccountId());
        this.accountVo.setMoney(Double.parseDouble(account.getMoney()));
        this.accountVo.setPassword(account.getPassword());
        this.accountVo.setSalt(account.getSalt());
        this.accountVo.setCreatedTime(account.getCreatedTime());
        return this.accountVo;
    }
    void initEmployeeVo(){
        this.employeeVo = new EmployeeVo();
        this.employeeVo.setSex("男");
        this.employeeVo.setBirthdate(new Date().toString());
        this.employeeVo.setIdentityNumber("123");
        this.employeeVo.setHeadPortrait("hello");
        this.employeeVo.setAge(20);
    }
    public EmployeeVo getEmployeeVo(User user,String roleName){
        initEmployeeVo();
        this.employeeVo.setId(user.getId());
        this.employeeVo.setUsername(user.getUsername());
        this.employeeVo.setPhoneNumber(user.getPhoneNumber());
        this.employeeVo.setSex(user.getSex());
        this.employeeVo.setAge(user.getAge());
        this.employeeVo.setBirthdate(user.getBirthdate());
        this.employeeVo.setHeadPortrait(user.getHeadPortrait());
        this.employeeVo.setIdentityNumber(user.getIdentityNumber());
        this.employeeVo.setHeadPortrait(user.getHeadPortrait());
        this.employeeVo.setRole(rolesMap.get(roleName));
        this.employeeVo.setCreatedTime(user.getCreatedTime());
        this.employeeVo.setUpdatedTime(user.getUpdatedTime());
        return this.employeeVo;
    }
    void initUserVo(){
        this.userVo = new UserVo();
    }
    public UserVo getUserVo(User user){
        initUserVo();
        this.userVo.setId(user.getId());
        this.userVo.setUsername(user.getUsername());
        this.userVo.setPhoneNumber(user.getPhoneNumber());
        this.userVo.setSex(user.getSex());
        this.userVo.setAge(user.getAge());
        this.userVo.setStatus(StatusEum.gertName(user.getStatus()));
        this.userVo.setBirthdate(user.getBirthdate());
        this.userVo.setHeadPortrait(user.getHeadPortrait());
        this.userVo.setIdentityNumber(user.getIdentityNumber());
        this.userVo.setHeadPortrait(user.getHeadPortrait());
        this.userVo.setCreatedTime(user.getCreatedTime());
        this.userVo.setUpdatedTime(user.getUpdatedTime());
        return this.userVo;
    }
    public UserVo getUserVo(String role,String name, String sex, String phoneNumber, String birthdate, String identityNumber, Integer age){
        initUserVo();
        this.userVo.setRoleName(role);
        this.userVo.setUsername(name);
        this.userVo.setPhoneNumber(phoneNumber);
        this.userVo.setSex(sex);
        this.userVo.setAge(age);
        this.userVo.setBirthdate(birthdate);
        this.userVo.setIdentityNumber(identityNumber);
        if(sex.equals("女")){
            this.userVo.setHeadPortrait("girl.png");
        }
        return this.userVo;
    }
    void initDepositListVo(){
        this.depositListVo = new DepositListVo();
    }
    public DepositListVo getDepositListVo(User user,Double money){
        initDepositListVo();
        this.depositListVo.setUserVo(getUserVo(user));
        this.depositListVo.setMoney(money);
        return this.depositListVo;
    }
    void initLoanListVo(){
        this.loanListVo = new LoanListVo();
    }
    public LoanListVo getLoanListVo(User user,Double totalMoney,Double paidMoney){
        initLoanListVo();
        this.loanListVo.setUserVo(getUserVo(user));
        this.loanListVo.setTotalMoney(totalMoney);
        this.loanListVo.setPaidMoney(paidMoney);
        return this.loanListVo;
    }
    void initPositionDistributionVo(){
        this.positionDistributionVo = new PositionDistributionVo();
    }
    public PositionDistributionVo getPositionDistributionVo(String roleName,int counts){
        initPositionDistributionVo();
        this.positionDistributionVo.setRoleName(roleName);
        this.positionDistributionVo.setCounts(counts);
        return this.positionDistributionVo;
    }
    void initUserBasicInfoVo(){
        this.userBasicInfoVo = new UserBasicInfoVo();
    }
    public UserBasicInfoVo getUserBasicInfoVo(User user){
        initUserBasicInfoVo();
        this.userBasicInfoVo.setId(user.getId());
        this.userBasicInfoVo.setUsername(user.getUsername());
        this.userBasicInfoVo.setPhoneNumber(user.getPhoneNumber());
        this.userBasicInfoVo.setAge(user.getAge());
        this.userBasicInfoVo.setHeadPortrait(user.getHeadPortrait());
        return this.userBasicInfoVo;
    }
    public List<UserBasicInfoVo> getUserBasicInfoVos(List<User> users){
        List<UserBasicInfoVo> userBasicInfoVos = new ArrayList<>();
        for(User user:users){
            userBasicInfoVos.add(getUserBasicInfoVo(user));
        }
        return userBasicInfoVos;
    }

}